package baidu.com.matedemo.pair;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.baidu.duer.libcore.util.ConsoleLogger;
import com.baidu.duer.libcore.util.ToastUtil;
import com.baidu.duer.smartmate.duerlink.bean.DuerlinkError;
import com.baidu.duer.smartmate.out.DuerDevice;
import com.baidu.duer.smartmate.out.DuerSDK;
import com.baidu.duer.smartmate.out.oauth.IResponseCallback;
import com.baidu.duer.smartmate.proxy.IConnectionListener;

/**
 * Created by sunjian11 on 18/3/1.
 */

public class PrivilegePairV3 implements IDevicePair, IConnectionListener {
    private static final int CONNECTION_TIMEOUT = 60 * 1000;
    private static final int OAUTH_RETRY_MAX_TIMES = 3;
    private static final int OAUTH_RETRY_INTERVAL = 3 * 1000;

    private Activity mActivity = null;
    private DuerDevice mDuerDevice = null;
    private IResponseCallback mCallback = null;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private int mRetryCount = 0;

    private Runnable mOauthRunnable = new Runnable() {
        @Override
        public void run() {
            mDuerDevice.privilegeOauth(mActivity, new IResponseCallback() {
                @Override
                public void onSuccess() {
                    ConsoleLogger.printInfo(PrivilegePairV3.class, "onSuccess");
                    mDuerDevice.connect(mActivity, PrivilegePairV3.this, CONNECTION_TIMEOUT);
                }

                @Override
                public void onError(long status, String msg) {
                    ConsoleLogger.printErrorInfo(PrivilegePairV3.class,
                            "onError " + status + " " + msg);
                    if (++mRetryCount < OAUTH_RETRY_MAX_TIMES) {
                        mHandler.postDelayed(mOauthRunnable, OAUTH_RETRY_INTERVAL);
                    } else {
                        mCallback.onError(DuerlinkError.OAUTH_FAIL.ordinal(),
                                DuerlinkError.OAUTH_FAIL.toString());
                        stopPair();
                    }
                }
            });
        }
    };

    @Override
    public void startPair(final Activity activity,
                          final DuerDevice duerDevice,
                          final IResponseCallback callback) {
        if (activity == null || duerDevice == null || callback == null) {
            return;
        }
        if (!DuerSDK.isLogin()) {
            ToastUtil.toastWarnning(activity, "请先登录");
            return;
        }
        mActivity = activity;
        mDuerDevice = duerDevice;
        mCallback = callback;
        mRetryCount = 0;
        mHandler.post(mOauthRunnable);
    }

    @Override
    public void stopPair() {
        if (mDuerDevice != null) {
            mDuerDevice.unregisterConnectionListener(this);
        }
    }

    @Override
    public void onConnected() {
        mCallback.onSuccess();
        stopPair();
    }

    @Override
    public void onLocalConnected() {

    }

    @Override
    public void onConnectionFailed() {
        mCallback.onError(DuerlinkError.DLP_CONNECT_FAIL.ordinal(),
                DuerlinkError.DLP_CONNECT_FAIL.toString());
        stopPair();
    }

    @Override
    public void onDisconnected() {

    }
}
